package com.zsc.wxm.ysh.anjo.house.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.wxm.ysh.anjo.house.cache.BaseDataCacheService;
import com.zsc.wxm.ysh.anjo.house.mapper.BaseDataMapper;
import com.zsc.wxm.ysh.anjo.house.model.BaseData;
import com.zsc.wxm.ysh.anjo.house.service.BaseDataService;
import com.zsc.wxm.ysh.anjo.result.CodeEnum;
import com.zsc.wxm.ysh.anjo.result.PageResult;
import com.zsc.wxm.ysh.anjo.service.base.exception.AnjoException;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.dto.BaseDataDTO;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.qo.BaseDataQuery;
import com.zsc.wxm.ysh.anjo.service.base.model.resources.vo.BaseDataVO;
import com.zsc.wxm.ysh.anjo.service.base.utils.BeanCopyUtil;
import com.zsc.wxm.ysh.anjo.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 *
 * @author playmaker
 * @since 2021/2/12 23:02
 */
@Service
@Slf4j
public class BaseDataServiceImpl extends ServiceImpl<BaseDataMapper, BaseData> implements BaseDataService {

    @Resource
    private BaseDataCacheService baseDataCacheService;

    @Override
    public void addBaseData(BaseDataDTO params) {
        log.info("传入的参数：{}", params);
        //校验
        if (dataIsExit(params)) {
            throw new AnjoException(CodeEnum.BASE_DATA_IS_EXIT);
        }
        if (sortNumIsExit(params)) {
            throw new AnjoException(CodeEnum.BASE_DATA_SORT_IS_EXIT);
        }
        BaseData baseData = new BaseData();
        BeanUtils.copyProperties(params, baseData);
        this.save(baseData);
        //清除缓存
        baseDataCacheService.removeByTypeCache(params.getType());
    }

    @Override
    public void updateBaseData(BaseDataDTO params) {
        if (sortNumIsExit(params)) {
            throw new AnjoException(CodeEnum.BASE_DATA_SORT_IS_EXIT);
        }
        BaseData baseData = new BaseData();
        BeanUtils.copyProperties(params, baseData);
        this.updateById(baseData);
        //清除缓存
        baseDataCacheService.removeByTypeCache(params.getType());
    }

    @Override
    public PageResult listPage(BaseDataQuery params) {
        Page<BaseData> page = new Page<>(params.getCurrPage(), params.getPageSize());
        LambdaQueryWrapper<BaseData> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.orderByAsc(BaseData::getSort);
        if (StringUtils.isNotBlank(params.getType())) {
            queryWrapper.like(BaseData::getType, params.getType());
        }
        if (StringUtils.isNotBlank(params.getKeyDesc())) {
            queryWrapper.like(BaseData::getKeyDesc, params.getKeyDesc());
        }
        if (params.getCreateTimeStart() != null) {
            queryWrapper.ge(BaseData::getCreateTime, params.getCreateTimeStart());
        }
        if (params.getCreateTimeEnd() != null) {
            queryWrapper.le(BaseData::getCreateTime, params.getCreateTimeEnd());
        }
        queryWrapper.orderByDesc(BaseData::getType);
        queryWrapper.orderByDesc(BaseData::getKeyDesc);
        queryWrapper.orderByAsc(BaseData::getSort);
        List<BaseDataVO> vos = new ArrayList<>();
        page = baseMapper.selectPage(page, queryWrapper);
        List<BaseData> list = page.getRecords();
        if (CollUtil.isNotEmpty(list)) {
            vos = BeanCopyUtil.copyListProperties(list, BaseDataVO::new);
        }
        PageUtil pageUtil = new PageUtil(page);
        return PageResult.ok(vos, pageUtil.getTotal());

    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

    @Override
    public List<BaseDataVO> listByType(String type) {
        return BeanCopyUtil.copyListProperties(baseDataCacheService.listByTypeCache(type),BaseDataVO::new);
    }

    @Override
    public BaseDataVO getOneById(Long id) {
        BaseDataVO baseDataVO = new BaseDataVO();
        BeanUtils.copyProperties(this.getById(id), baseDataVO);
        return baseDataVO;
    }

    /**
     * 判断数据是否已存在
     *
     * @param params
     * @return
     */
    private boolean dataIsExit(BaseDataDTO params) {
        LambdaQueryWrapper<BaseData> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BaseData::getType, params.getType()).eq(BaseData::getKeyDesc, params.getKeyDesc());
        return CollUtil.isNotEmpty(this.list(queryWrapper));
    }

    /**
     * 判断序号是否重复
     *
     * @param params
     * @return
     */
    private boolean sortNumIsExit(BaseDataDTO params) {
        LambdaQueryWrapper<BaseData> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BaseData::getType, params.getType()).eq(BaseData::getKeyDesc, params.getKeyDesc())
                .eq(BaseData::getSort, params.getSort());
        BaseData baseData = this.list(queryWrapper)
                .stream()
                 //修改的时候排除自己
                .filter(x -> !x.getId().equals(params.getId()))
                .findFirst()
                .orElse(null);
        return baseData != null;
    }




}
